﻿function ShowClientDivAddnew() {
    document.getElementById("divProcessing").innerHTML = "Hệ thống đang xử lý";
    $("#divshowhide").html(Portal.SODC.IDIS.CompaniesInfo.GetHtmlToAddnew().value);
    document.getElementById("divProcessing").innerHTML = "";
}

function ClientSideUpdateCompanyInfo(companyid, belongsite, MaxItems, ItemsPerRow, NoOfVisiblePage, CurrentPage, JumpJavaScript) {
    comName = document.getElementById("comName").value;
    if (comName == "") {
        alert("Chưa nhập tên công ty!");
        document.getElementById("comName").focus();
        return;
    }
    Trademark = document.getElementById("Trademark").value;
    CompanyCode = document.getElementById("CompanyCode").value;
    if (CompanyCode == "") {
        alert("Chưa nhập số đăng ký kinh doanh của công ty!");
        document.getElementById("CompanyCode").focus();
        return;
    }
    Address = document.getElementById("Address").value;
    if (Address == "") {
        alert("Chưa nhập địa chỉ công ty!");
        document.getElementById("Address").focus();
        return;
    }

    DateFoundation = document.getElementById("drpDayDateFoundation").options[document.getElementById("drpDayDateFoundation").selectedIndex].value + '/' + document.getElementById("drpMonthDateFoundation").options[document.getElementById("drpMonthDateFoundation").selectedIndex].value + '/' + document.getElementById("drpYearDateFoundation").options[document.getElementById("drpYearDateFoundation").selectedIndex].value;
   // DateFoundation = document.getElementById("DateFoundation").value;
//    if (DateFoundation == "") {
//        alert("Chưa nhập ngày thành lập công ty!");
//        document.getElementById("DateFoundation").focus();
//        return;
//    }
//    checkDateFoundation = checkdate(DateFoundation);
//    if (checkDateFoundation == false) {
//        document.getElementById("DateFoundation").select();
//        return;
//    }

    document.getElementById("divProcessing").innerHTML = "Hệ thống đang xử lý";
    retValue = Portal.SODC.IDIS.CompaniesInfo.ServerSideUpdateCompayInfo(companyid, comName, Trademark, CompanyCode, Address, DateFoundation).value;
    if (retValue.indexOf("ERROR") != -1) {
        document.getElementById("divProcessing").innerHTML = "";
        alert(retValue.replace("ERROR:", ""));
        return;
    }
    document.getElementById("divProcessing").innerHTML = "";
    alert(retValue);
    ClientHideDiv();
    RealFilterFunc(belongsite, MaxItems, ItemsPerRow, NoOfVisiblePage, CurrentPage, JumpJavaScript); 
}

function ClientSideAddnewCompanyInfo() {
    comName = document.getElementById("comName").value;
    if (comName == "") {
        alert("Chưa nhập tên công ty!");
        document.getElementById("comName").focus();
        return;
    }
    Trademark = document.getElementById("Trademark").value;
    CompanyCode = document.getElementById("CompanyCode").value;
    if (CompanyCode == "") {
        alert("Chưa nhập số đăng ký kinh doanh của công ty!");
        document.getElementById("CompanyCode").focus();
        return;
    }
    Address = document.getElementById("Address").value;
    if (Address == "") {
        alert("Chưa nhập địa chỉ công ty!");
        document.getElementById("Address").focus();
        return;
    }
    DateFoundation = document.getElementById("drpDayDateFoundation").options[document.getElementById("drpDayDateFoundation").selectedIndex].value + '/' + document.getElementById("drpMonthDateFoundation").options[document.getElementById("drpMonthDateFoundation").selectedIndex].value + '/' + document.getElementById("drpYearDateFoundation").options[document.getElementById("drpYearDateFoundation").selectedIndex].value;
//    DateFoundation = document.getElementById("DateFoundation").value;
//    if (DateFoundation == "") {
//        alert("Chưa nhập ngày thành lập công ty!");
//        document.getElementById("DateFoundation").focus();
//        return;
//    }
//    checkDateFoundation = checkdate(DateFoundation);
//    if (checkDateFoundation == false) {
//        document.getElementById("DateFoundation").select();
//        return;
//    }

    document.getElementById("divProcessing").innerHTML = "Hệ thống đang xử lý";
    retValue = Portal.SODC.IDIS.CompaniesInfo.ServerSideAddnewCompayInfo(comName, Trademark, CompanyCode, Address, DateFoundation).value;
    if (retValue.indexOf("ERROR") != -1) {
        document.getElementById("divProcessing").innerHTML = "";
        alert(retValue.replace("ERROR:", ""));
        return;
    }
    //document.getElementById("divProcessing").innerHTML = "";
    alert(retValue);
    ClientResetAllFields();
    FilterFunc();
}

function ClientHideDiv() {
    document.getElementById("divProcessing").innerHTML = "Hệ thống đang xử lý";
    $("#divshowhide").html("");
    document.getElementById("divProcessing").innerHTML = "";
}

function ClientResetAllFields() {
    document.getElementById("comName").value = "";
    document.getElementById("Trademark").value="";
    document.getElementById("CompanyCode").value="";
    document.getElementById("Address").value = "";
    //DateFoundation = document.getElementById("drpDayDateFoundation").options[document.getElementById("drpDayDateFoundation").selectedIndex].value + '/' + document.getElementById("drpMonthDateFoundation").options[document.getElementById("drpMonthDateFoundation").selectedIndex].value + '/' + document.getElementById("drpYearDateFoundation").options[document.getElementById("drpYearDateFoundation").selectedIndex].value;
    document.getElementById("drpDayDateFoundation").selectedIndex = 0;
    document.getElementById("drpMonthDateFoundation").selectedIndex = 0;
    document.getElementById("drpYearDateFoundation").selectedIndex = 0;
    document.getElementById("comName").focus();
}

function checkdate(date) {
    var validformat = /^\d{2}\/\d{2}\/\d{4}$/;
    var returnval = false;
    if (!validformat.test(date))
        alert("Nhập sai định dạng ngày tháng.");
    else {
        var dayfield = date.split("/")[0];
        var monthfield = date.split("/")[1];
        var yearfield = date.split("/")[2];
        var dayobj = new Date(yearfield, monthfield - 1, dayfield);
        if ((dayobj.getMonth() + 1 != monthfield) || (dayobj.getDate() != dayfield) || (dayobj.getFullYear() != yearfield))
            alert("Vui lòng nhập lại ngày tháng theo định dạng ngày/tháng/năm.");
        else
            returnval = true;
    }
    return returnval;
}

function FilterFunc() {
    document.getElementById("divProcessing").innerHTML = "Hệ thống đang xử lý";
    setTimeout("RealFilterFunc('', 0, 30, 10, 0, 'RealFilterFunc')", 10);
}

function RealFilterFunc(belongsite, MaxItems, ItemsPerRow, NoOfVisiblePage, CurrentPage, JumpJavaScript) {
    keywords = document.getElementById("keywords").value;
    retValue = Portal.SODC.IDIS.CompaniesInfo.GetHtmlResult(keywords, ItemsPerRow, NoOfVisiblePage, CurrentPage, JumpJavaScript).value;
    if (retValue.indexOf("ERROR") != -1) {
        document.getElementById("divProcessing").innerHTML = "";
        alert(retValue.replace("ERROR:", ""));
        return;
    }
    document.getElementById("divProcessing").innerHTML = "";
    document.getElementById("resultcontent").innerHTML = retValue;
}

function ShowClientUpdateUrlForm(recordid, belongsite, MaxItems, ItemsPerRow, NoOfVisiblePage, CurrentPage, JumpJavaScript) {
    document.getElementById("divProcessing").innerHTML = "Hệ thống đang xử lý";

    $("#divshowhide").html(Portal.SODC.IDIS.CompaniesInfo.GetShowUpdateForm(recordid, belongsite, MaxItems, ItemsPerRow, NoOfVisiblePage, CurrentPage, JumpJavaScript).value);
    document.getElementById("divProcessing").innerHTML = "";    
}

function ShowClientViewUrlForm(recordid, belongsite, MaxItems, ItemsPerRow, NoOfVisiblePage, CurrentPage, JumpJavaScript) {
    document.getElementById("divProcessing").innerHTML = "Hệ thống đang xử lý";

    $("#divshowhide").html(Portal.SODC.IDIS.CompaniesInfo.GetShowViewForm(recordid, belongsite, MaxItems, ItemsPerRow, NoOfVisiblePage, CurrentPage, JumpJavaScript).value);
    document.getElementById("divProcessing").innerHTML = "";
}

function ClientDeleteCompanies(recordid, belongsite, MaxItems, ItemsPerRow, NoOfVisiblePage, CurrentPage, JumpJavaScript) {
    Msg = Portal.SODC.IDIS.CompaniesInfo.GetConfirmDeleteMsg().value;
    if (confirm(Msg) == false) return;
    document.getElementById("divProcessing").innerHTML = "Hệ thống đang xử lý";
    RetValue = Portal.SODC.IDIS.CompaniesInfo.ServerSideDeleteCompanies(recordid).value;
    if (RetValue.indexOf("ERROR") != -1) {
        document.getElementById("divProcessing").innerHTML = "";
        alert(RetValue.replace("ERROR:", ""));
        return;
    }
    document.getElementById("divProcessing").innerHTML = "";
    alert(RetValue);
    RealFilterFunc(belongsite, MaxItems, ItemsPerRow, NoOfVisiblePage, CurrentPage, JumpJavaScript);
}
function ClientExportData() {
    keywords = document.getElementById("keywords").value;
    Url = Portal.SODC.IDIS.CompaniesInfo.ServerExportData(keywords).value;
    if (Url.indexOf("ERROR") != -1) {
        document.getElementById("divProcessing").innerHTML = "";
        alert(Url.replace("ERROR:", ""));
        return;
    }
    document.getElementById("divProcessing").innerHTML = "";
    window.open(Url,"_blank")
}
